package com.teste;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.jdo.PersistenceManager;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Dados extends HttpServlet {

	/** 
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {

		} finally {
			out.close();
		}
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<head>");
		out.println("<title>Servlet Form_Alunos</title>");  
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>Servlet Dados</h1>");

		PersistenceManager pm = PMF.get().getPersistenceManager();
		String query = "select from " + Empregado.class.getName();
		List<Empregado> emp = (List<Empregado>) pm.newQuery(query).execute();
		if (emp.isEmpty()) {
			out.println("ups nao BD!!");
		} else {
			out.println(emp.size()+"\n");
			for (Empregado e : emp) {
				
				out.println(e.getNome().toString() + " - " + e.getCurso().toString()+ " - " + e.getEmail().toString()+"\n");
			}
		}
		pm.close();

		out.println("</body>");
		out.println("</html>");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		doGet(request, response);
	}

	/** 
	 * Returns a short description of the servlet.
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo() {
		return "Short description";
	}

}
